<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class BusinessesController extends Controller
{
    //index
 	 public function getIndex(Request $request)
 	 {
 	 	//查询
 	 	$data = DB::table('businesses')
 	 		-> where(function($query) use ($request){
 	 			$query -> where('username', 'like', '%'.$request -> input('keyword').'%');
 	 		})
 	 		-> paginate($request -> input('num', 1));

 	 	return view('admin.businesses.index', ['data' => $data, 'request' =>$request -> all()]);
 	 }

 	 // 编辑
	 public function getEdit($id)
	 {
	 	// return $id;
	 	$data = DB::table('businesses') -> where('id', $id) -> first();
	 	$cates = DB::table('cates') -> where('id', 1) -> first();//需要改动
	    // $cate = $cates -> only('name');
	 	// dd($cates);
	 	return view('admin.businesses.edit', ['data'=> $data, 'cates'=> $cates]);
	 }


	 //更新
	 public function postUpdate(Request $request)
	 {
	 	// dd($request -> all());
	 	$this -> validate($request,[
	 			'username' => 'required',
	 		],[
	 			'username.required' => '用户名不能为空',
	 		]);
	 	$data = $request ->except('_token', 'id');
	 	$data['updated_at'] = date('Y-m-d H:i:s');

	 	// 执行更新
	 	$res = DB::table('businesses') -> where('id', $request -> input('id'))-> update($data);
	 	//判断
	 	if($res)
	 	{
	 		return redirect('/admin/businesses/index') -> with(['info' => '更新成功']);
	 	}else
	 	{
	 		return back() -> with(['info' => '更新失败']);
	 	}
	 }

	 //删除
	 public function getDelete($id)
	 {
	 	// return $id;
	 	$res = DB::table('businesses') -> where('id', $id) -> delete();
	 	//判断
	 	if($res)
	 	{
	 		return redirect('/admin/businesses/index') -> with(['info' => '删除成功']);
	 	}else
	 	{
	 		return back() -> with(['info' => '删除失败']);
	 	}
	 }
}
